#include "EditorWindowGroupView.h"
#include "Framework/Editor.h"

#include "EditorWindowGroupViewModel.h"

#include "Window/EditorMainWindowView.h"
#include "Window/EditorMainWindowViewModel.h"
#include "Window/EditorToolTipWindowView.h"
#include "Window/EditorContextMenuView.h"
#include "Window/EditorPopupMenuView.h"

static REditorWindowGroupView*
    GEditorWindowGroupView = nullptr;

void REditorWindowGroupView::Initialize()
{
    GEditorWindowGroupView = this;

    for(auto& RegisteredWindow: REditorWindowGroupViewModel::Get()->GetWindows())
    {
        auto View = CreateWindowView(RegisteredWindow);
        RegisterWindow(View);
    }

    //bind delegates
    REditorWindowGroupViewModel::Get()->OnWindowRegistered.Add([this](TSharedObjectPtr<REditorWindowViewModel> Window)
    {
        auto View = CreateWindowView(Window);
        RegisterWindow(View);
    }); 

    REditorWindowGroupViewModel::Get()->OnWindowUnregistered.Add([this](TSharedObjectPtr<REditorWindowViewModel> Window)
    {
        auto Views=Windows;
        for(auto View: Views)
        {
            if(View->GetViewModel() == Window)
            {
                UnregisterWindow(View);
                break;
            }
        }
    });

    Tooltip=NewObject<REditorToolTipWindowView>(this);
    Tooltip->BindViewModel(REditorWindowGroupViewModel::Get()->GetTooltip());

    ContextMenu=NewObject<REditorContextMenuView>(this);
    ContextMenu->BindViewModel(REditorWindowGroupViewModel::Get()->GetContextMenu());

    PopupMenu=NewObject<REditorPopupMenuView>(this);
}

void REditorWindowGroupView::Deinitialize()
{
    GEditorWindowGroupView = nullptr;
}

REditorWindowGroupView *REditorWindowGroupView::Get()
{
    return GEditorWindowGroupView;
}

void REditorWindowGroupView::RegisterWindow(TSharedObjectPtr<REditorWindowView> WindowView)
{
    Windows.Add(WindowView);
}

void REditorWindowGroupView::UnregisterWindow(TSharedObjectPtr<REditorWindowView> WindowView)
{
    WindowView->UnbindViewModel();
    Windows.Remove(WindowView);
}

void REditorWindowGroupView::CloseWindow(TSharedObjectPtr<REditorWindowView> WindowView)
{
    REditorWindowGroupViewModel::Get()->CloseWindow(WindowView->GetViewModel());
}

TSharedObjectPtr<RWidget> REditorWindowGroupView::CreatePopupMenu(const SIntPoint &InMeneSize, TSharedObjectPtr<RWidget> InWidgetToAround)
{
    return PopupMenu->ShowPopupMenu(InMeneSize, InWidgetToAround);
}

void REditorWindowGroupView::ClosePopupMenu()
{
    PopupMenu->ClosePopupMenu();
}

TSharedObjectPtr<REditorWindowView> REditorWindowGroupView::CreateWindowView(TSharedObjectPtr<REditorWindowViewModel> WindowViewModel)
{
    CClass* ViewModelClass = WindowViewModel->GetClass();
    
    CClass* ViewClass = REditor::Get()->GetViewClass(ViewModelClass);

    auto View = NewObject<REditorWindowView>(ViewClass, this);
    View->BindViewModel(WindowViewModel);

    REditorWindowViewWeakPtr WeakView = View;
    View->OnCloseButtonClicked.Bind([this, WeakView]()
    {
        CloseWindow(WeakView);
    });

    return View;
    
}
